package com.mjk.common.core.jdbc.util.provider;

import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.core.jdbc.annotation.Transient;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.ReflectionUtils;
import com.mjk.common.util.StrUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class InsertSqlProvider extends SqlProvider {
    public static String getInsertFiled(BaseEntity t) {
        return getInsertFiled(t, true);
    }

    /**
     * 获取insert字段
     */
    public static String getInsertFiled(BaseEntity t, boolean canNull) {
        List<String> getField = new ArrayList<>();
        List<Field> fields = getValidTableFiled(t, true);
        for (Field f : fields) {
            Object o = ReflectionUtils.getFieldValue(t, f.getName());
            if (ObjectUtils.isNotEmpty(o) || canNull)
                getField.add(getCloumName(f));
        }
        return StrUtils.join(getField, ",");
    }

    /**
     * 获取insert字段的值
     */
    public static String getInsertFiledValue(BaseEntity t) {
        return getInsertFiledValue(t, true);
    }

    public static String getInsertFiledValue(BaseEntity t, boolean canNull) {
        List<String> sql = new ArrayList<>();
        List<Field> fields = SqlProvider.getValidTableFiled(t, true);
        for (Field f : fields) {
            Object o = ReflectionUtils.getFieldValue(t, f.getName());
            if (ObjectUtils.isNotEmpty(o) || canNull) {
                sql.add(getFiledType("#{item.", t, f));
            } else {
                sql.add("#{item." + f.getName() + ",jdbcType=VARCHAR}");
            }
        }
        return StrUtils.join(sql, ",");
    }
}
